package com.fansl.allround.open.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.fansl.allround.common.core.exception.CheckedException;
import com.fansl.allround.common.core.validator.MobileValidator;
import com.fansl.allround.common.core.validator.NotNullValidator;
import com.fansl.allround.open.api.constant.enums.juhe.NewsTypeEnum;
import com.fansl.allround.open.api.entity.sms.SendSmsCodePO;
import com.fansl.allround.open.api.vo.NewsHeadlinesVO;
import com.fansl.allround.open.api.vo.SmsCodeVO;
import com.fansl.allround.open.api.vo.WeatherVO;
import com.fansl.allround.open.service.JuHeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author fansl
 * @Description: TODO
 * @date 2019/9/10 11:06
 */
@RestController
@RequestMapping("/juhe")
public class JuHeController {
    @Autowired
    private JuHeService juHeService;

    @GetMapping("/newsHeadlines/{newsType}")
    public List<NewsHeadlinesVO> newsHeadlines(@PathVariable String newsType){
        NewsTypeEnum newsTypeEnum = NewsTypeEnum.getByDesc(newsType.trim());
        return juHeService.getNewsHeadlines(newsTypeEnum);
    }
    @GetMapping("/weather")
    public WeatherVO weather(@RequestParam String cityName){
        ComplexResult result = FluentValidator.checkAll()
                .on(cityName,new NotNullValidator("短信验证码类型"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if(!result.isSuccess()){
            throw new CheckedException(result.getErrors().get(0).getErrorMsg());
        }
        return juHeService.getWeather(cityName);
    }

    @PostMapping("/sms")
    public SmsCodeVO smsCode(@RequestBody SendSmsCodePO smsCodePO){
        ComplexResult result = FluentValidator.checkAll()
                .on(smsCodePO.getMobile(),new MobileValidator())
                .on(smsCodePO.getSmsCodeType(),new NotNullValidator("短信验证码类型"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if(!result.isSuccess()){
            throw new CheckedException(result.getErrors().get(0).getErrorMsg());
        }
        return juHeService.sendSmsCode(smsCodePO);
    }
}
